package com.tensquare.gathering.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.tensquare.gathering.pojo.Gathering;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 数据访问接口
 * @author Administrator
 *
 */
public interface GatheringDao extends JpaRepository<Gathering,String>,JpaSpecificationExecutor<Gathering>{
    //活动报名
    @Modifying
	@Query(value = "insert into tb_usergath values(?,?,NOW())",nativeQuery = true)
    public int signup(String uid,String gid);

    //取消活动报名
    @Modifying
    @Query(value = "delete from tb_usergath where userid=? and gathid=?",nativeQuery = true)
    public int delsignup(String uid, String gid);

    //查询是否已经报名
    @Query(value = "select COUNT(*) from tb_usergath where userid=? and gathid=?",nativeQuery = true)
    public int signed(String uid,String gid);

    //按时间排序查询最新的前四个的活动列表
    @Query(value = "select * from tb_gathering order by starttime desc limit 4",nativeQuery = true)
    public List<Gathering> findTop4ByStarttime();

    //查询我的活动报名列表
    @Query(value = "select gathid from tb_usergath where userid=?",nativeQuery = true)
    public List<String> getMyGatherings(String me);
}
